<template>
	<view class="index">
		<view class="header" style="height: 30rpx;">
			<view style="background:#00217D"></view>
			<view class="top" style="display: flex;">
				<view class="null"></view>
			</view>
		</view>
		<view class="header">
			<view style="background:#00217D"></view>
			<view class="top" style="display: flex;">
				<view @click="signMoney" class="sign_money">签到领钱</view>
				<view class="live">万象生活菜场</view>
				<view class="null"></view>
			</view>
		</view>
		<view class="content">
			<view class="page-section-spacing">
				<swiper indicator-color="rgba(255,255,255,0.3)" indicator-active-color="rgba(255,255,255,1)" class="swiper"
				 indicator-dots="true" autoplay="true" interval="3000" duration="1500">
					<swiper-item v-for="(item , index) in background" :key="index">
						<image :src="item.bannerImage" mode="aspectFill"></image>
					</swiper-item>
				</swiper>
			</view>
			<view class="main">
				<view class="main_box" v-for="(item,index) in mainlist" :key="index">
					<image :src="item.img"></image>
				</view>
			</view>
			<!--生鲜推荐-->
			<view class="recommend">
				<!-- 推荐头部 -->
				<view class="recommend_header">
					<text>生鲜推荐</text>
					<text decode="true">点击查看更多&gt;&gt;</text>
				</view>
				<view class="recommend_main">
					<view class="recommend_main_box" v-for="(item,index) in freshList" :key="index">
						<image :src="item.images"></image>
						<view class="recommend_main_box_main">
							<view style="margin:4rpx 0;">{{ item.commodityName }}</view>
							<view>￥<text>{{item.currentPrice}}</text>月售{{ item.salesVolume }}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="recommend">
				<view class="recommend_header">
					<text style="color: #FFC30E;">团购推荐</text>
					<text decode="true">点击查看更多&gt;&gt;</text>
				</view>
				<view class="recommend_main">
					<view class="recommend_main_box" v-for="(item,index) in groupList" :key="index">
						<image :src="item.images"></image>
						<view class="recommend_main_box_main">
							<view style="margin:4rpx 0;">{{ item.commodityName }}</view>
							<view>￥<text>{{item.currentPrice}}</text>月售{{ item.salesVolume }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				background: [],
				mainlist: [{
					img: 'https://obs-4a40.obs.cn-southwest-2.myhuaweicloud.com/shop/shu.png'
				}, {
					img: 'https://obs-4a40.obs.cn-southwest-2.myhuaweicloud.com/shop/hanbao.png'
				}], //首页图片
				longitude: 104.10194,
				latitude: 30.65984,
				freshList: [],
				groupList: [],
				recommendList: [
					// 生鲜推荐
					{
						id: 0,
						header: '生鲜推荐',
						mainList: [{
								imgUrl: '../../static/images/推荐@2x.png',
								name: '生鲜四川莴笋新鲜青莴笋嘿嘿嘿嘿嘿嘿嘿和hi',
								money: 32.8,
							},
							{
								imgUrl: '../../static/images/推荐@2x.png',
								name: '生鲜四川莴笋新鲜青莴笋嘿嘿嘿嘿嘿嘿嘿和hi',
								money: 32.8,
							},
						],
					},
					// 团购推荐
					{
						id: 1,
						header: '团购推荐',
						mainList: [{
								imgUrl: '../../static/images/推荐@2x.png',
								name: '生鲜四川莴笋新鲜青莴笋嘿嘿嘿嘿嘿嘿嘿和hi',
								money: 32.8,
							},
							{
								imgUrl: '../../static/images/推荐@2x.png',
								name: '生鲜四川莴笋新鲜青莴笋嘿嘿嘿嘿嘿嘿嘿和hi',
								money: 32.8,
							},
						],
					},
				], //推荐循环
			}
		},
		onLoad() {
			this.getBanner();
			// this.getFresh();
			// this.getGroup();
			this.judge();
		},
		methods: {
			//判断是否授权地理位置
			judge(){
				let that=this;
				wx.getSetting({
				  success(res) {
				    if (!res.authSetting['scope.userLocation']) {
				      wx.authorize({
				        scope: 'scope.userLocation',
				        success () {
				          that.getLocation();
				        }
				      })
				    }else{
							that.getLocation();
						}
				  }
				})
			},
			getLocation() {
				let that=this;
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						console.log(res)
						that.longitude=res.longitude;
						that.latitude=res.latitude;
						that.getFresh();
						that.getGroup();
					},
					fail: function(err) {
						console.log(err);
						that.getFresh();
						that.getGroup();
					}
				})
			},
			//获取综合首页生鲜推荐商品
			getFresh() {
				const requestParam = {
					url: "/api/commodity/index/list",
					method: "POST",
					data: {
						vid: 0,
						index: 1,
						size: 2,
						longitude: this.longitude,
						latitude: this.latitude
					},
					callBack: (res) => {
						this.freshList = res.data;
					}
				}
				this.$http.request(requestParam);
			},
			//获取综合首页团购推荐商品
			getGroup() {
				const requestParam = {
					url: "/api/commodity/index/list",
					method: "POST",
					data: {
						vid: 2,
						index: 1,
						size: 2,
						longitude: this.longitude,
						latitude: this.latitude
					},
					callBack: (res) => {
						this.groupList = res.data;
					}
				}
				this.$http.request(requestParam);
			},
			//获取综合首页轮播图
			getBanner() {
				const requestParam = {
					url: "/api/commodity/banner/list",
					method: "POST",
					data: {
						vid: 0,
						index: 1,
						size: 3
					},
					callBack: (res) => {
						this.background = res.data;
					}
				}

				this.$http.request(requestParam);
			},
			signMoney() {
				uni.navigateTo({
					url: "../signIn/signIn"
				})
			}
		},
	}
</script>

<style lang="scss">
	.index {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: #EEEEEE;
	}

	.top {
		height: 88rpx;
		background: #01227e;
		display: flex;
		align-items: center;
		color: #ffffff;
		font-size: 28rpx;
	}

	.sign_money {
		padding-left: 20rpx;
	}

	.live {
		font-size: 34rpx;
		font-weight: bold;
		flex: 1;
		text-align: center;
	}

	.null {
		width: 200rpx;
	}

	.swiper {
		width: 100%;
		height: 250rpx;
	}

	.swiper image {
		width: 750rpx;
		height: 250rpx;
	}

	.main image {
		padding: 10rpx 20rpx;
		width: 710rpx;
		height: 400rpx;
	}

	.recommend {
		width: 710rpx;
		background: #fff;
		border-radius: 8rpx;
		height: 370rpx;
		box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.16);
		padding: 0 20rpx;
		box-sizing: border-box;
		margin: 0 auto 20rpx;
	}

	/* 推荐头部 */
	.recommend_header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10rpx 0;
	}

	text:first-child {
		font-size: 34rpx;
		font-weight: bold;
		color: #86c958;
	}

	text:nth-child(2) {
		font-size: 24rpx;
		font-weight: 400;
		color: #999;
	}

	/* 推荐主体 */
	.recommend_main {
		display: flex;
	}

	.recommend_main_box {
		width: 328rpx;
		margin-right: 14rpx;
	}

	.recommend_main_box image {
		height: 202rpx;
		width: 100%;
	}


	.recommend_main_box_main {
		padding: 0 20rpx;
	}

	.recommend_main_box_main view:first-child {
		font-size: 24rpx;
		font-weight: bold;
		width: 100%;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.recommend_main_box_main view:nth-child(2) {
		font-size: 20rpx;
		color: #999;
		font-weight: bold;
	}

	.recommend_main_box_main view:nth-child(2) text {
		color: #ff6633;
		font-size: 34rpx;
		font-weight: bold;
		padding-right: 16rpx;
	}

	/* 循环的第二个推荐的字体 */
	.recommend:nth-child(4) .recommend_header text:first-child {
		color: #ffc30e;
	}

	.content {
		flex: 1;
		overflow-y: scroll;
	}
</style>
